# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=gdb

_gcc_ver=4.9.0
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=7.7.1
pkgrel=1
pkgdesc="GNU Debugger (mingw-w64)"
arch=('any')
url="http://www.gnu.org/software/gdb/"
license=('GPL')
groups=("${MINGW_PACKAGE_PREFIX}-toolchain")
depends=("${MINGW_PACKAGE_PREFIX}-libiconv"
    "${MINGW_PACKAGE_PREFIX}-zlib"
    "${MINGW_PACKAGE_PREFIX}-expat"
    "${MINGW_PACKAGE_PREFIX}-libiconv"
    "${MINGW_PACKAGE_PREFIX}-python2")
checkdepends=('dejagnu' 'bc')
makedepends=("${MINGW_PACKAGE_PREFIX}-libiconv" "${MINGW_PACKAGE_PREFIX}-zlib" "${MINGW_PACKAGE_PREFIX}-ncurses" "${MINGW_PACKAGE_PREFIX}-xz")
options=('staticlibs' '!distcc' '!ccache')
source=(http://ftp.gnu.org/gnu/gdb/gdb-${pkgver}.tar.bz2{,.sig}
        'gdbinit'
        'gdb-fix-display-tabs-on-mingw.patch'
        'gdb-mingw-gcc-4.7.patch'
        'gdb-perfomance.patch'
        'gdb-fix-using-gnu-print.patch'
        '020-add-bigobj-format.patch')
md5sums=('77b20b515e7c25f032cb9732a66620fe'
         'SKIP'
         'dec2439447cb1accf037457b459093f7'
         'e48292d553379a9765c82e80e618ffd2'
         '7cc5df72d958ef3e4b1ffa9f7b069e6f'
         'e9b7c252fedfa950998f5f3b32536461'
         'd7112b220466a294aa266bb4d5c06a59'
         '9b596cfe3a448dc5823370173349d88b')

prepare() {
  cd ${srcdir}/gdb-${pkgver}

  # https://sourceware.org/ml/gdb-patches/2013-11/msg00224.html
  #patch -p1 -i ${srcdir}/gdb-fix-display-tabs-on-mingw.patch
  # https://sourceware.org/bugzilla/show_bug.cgi?id=15559
  patch -p1 -i ${srcdir}/gdb-mingw-gcc-4.7.patch
  # http://sourceware.org/bugzilla/show_bug.cgi?id=15412
  patch -p1 -i ${srcdir}/gdb-perfomance.patch

  patch -p1 -i ${srcdir}/gdb-fix-using-gnu-print.patch
  patch -p1 -i ${srcdir}/020-add-bigobj-format.patch

  # hack! - libiberty configure tests for header files using "$CPP $CPPFLAGS"
  sed -i "/ac_cpp=/s/\$CPPFLAGS/\$CPPFLAGS -O2/" libiberty/configure
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir ${srcdir}/build-${MINGW_CHOST}
  cd ${srcdir}/build-${MINGW_CHOST}

  CFLAGS+=" -D__USE_MINGW_ANSI_STDIO=1"
  CXXFLAGS+=" -D__USE_MINGW_ANSI_STDIO=1"

  ${srcdir}/gdb-${pkgver}/configure \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    --enable-targets="i686-w64-mingw32,x86_64-w64-mingw32" \
    --enable-64-bit-bfd \
    --disable-werror \
    --disable-win32-registry \
    --disable-rpath \
    --with-system-gdbinit=${MINGW_PREFIX}/etc/gdbinit \
    --with-python=${MINGW_PREFIX}/bin/python-config-u.sh \
    --with-expat \
    --with-libiconv-prefix=${MINGW_PREFIX} \
    --with-zlib \
    --with-lzma \
    --disable-tui

  make
}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}
  make DESTDIR=${pkgdir} install

  # Remove unwanted files
  rm -rf ${pkgdir}${MINGW_PREFIX}/share/{man,info}

  # install "custom" system gdbinit
  install -D -m644 ${srcdir}/gdbinit ${pkgdir}${MINGW_PREFIX}/etc/gdbinit
  sed -i 's|%GCC_NAME%|gcc-'${_gcc_ver}'|g' ${pkgdir}${MINGW_PREFIX}/etc/gdbinit

  rm -f ${pkgdir}${MINGW_PREFIX}/include/*.h
  rm -f ${pkgdir}${MINGW_PREFIX}/lib/*.a
}
